package com.tjec.project.esb.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjec.common.constant.HttpStatus;
import com.tjec.common.enums.NumCode;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.esb.domain.ESBAjaxResult;
import com.tjec.project.esb.domain.EsbResHead;
import com.tjec.project.projectWorkbench.domain.BmsAgencyNo;
import com.tjec.project.projectWorkbench.domain.BmsCostNo;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.domain.BmsReportNo;
import com.tjec.project.projectWorkbench.mapper.BmsProjectMapper;
import com.tjec.project.projectWorkbench.service.IBmsAgencyNoService;
import com.tjec.project.projectWorkbench.service.IBmsCostNoService;
import com.tjec.project.projectWorkbench.service.IBmsReportNoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 *
 * 供esb调用接口类
 * @author ：cjj
 * @date ：Created in 2020/10/15 14:37
 */
@RestController
@RequestMapping("/esb/api")
public class ESBApiController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ESBApiController.class);

    @Autowired
    private IBmsAgencyNoService bmsAgencyNoService;

    @Autowired
    private IBmsCostNoService bmsCostNoService;

    @Autowired
    private IBmsReportNoService bmsReportNoService;

    @Autowired
    private BmsProjectMapper bmsProjectMapper;

/*    @Value("${ESB.serviceName}")
    private String esbServiceName;

    @Value("${ESB.sourceSystem}")
    private String esbSourceSystem;

    @Value("${ESB.targetSystem}")
    private String esbTargetSystem;*/

    /**
     * 修改招标代理号
     */
    @Log(title = "修改招标代理号", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateAgencyNo",produces = "application/json;charset=utf-8")
    public ESBAjaxResult pushAgencyNo(HttpServletResponse response, @RequestBody Map<String, Object> map){
        ObjectMapper objectMapper = new ObjectMapper();
        BmsAgencyNo bmsAgencyNo = objectMapper.convertValue(map.get(ESBAjaxResult.ESB_REQ_DATA), BmsAgencyNo.class);
        if(StringUtils.isBlank(bmsAgencyNo.getAgencyNo())){
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
            return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), "招标代理号不存在或不能为空"),null);
        }
        int result = bmsAgencyNoService.updateBmsAgencyNoByAgencyNo(bmsAgencyNo);
        if(result <= 0 ){
            result = bmsAgencyNoService.updateBmsAgencyNoHistoryByAgencyNo(bmsAgencyNo);
        }
        return toResult(response, result, map);
    }

    /**
     * 修改造价报告号
     */
    @Log(title = "修改造价报告号", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateCostNo",produces = "application/json;charset=utf-8")
    public ESBAjaxResult costNo(HttpServletResponse response, @RequestBody Map<String, Object> map)
    {
        ObjectMapper objectMapper = new ObjectMapper();
        BmsCostNo bmsCostNo = objectMapper.convertValue(map.get(ESBAjaxResult.ESB_REQ_DATA), BmsCostNo.class);
        if(StringUtils.isBlank(bmsCostNo.getCostNo())){
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
            return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), "造价报告号不存在或不能为空"),null);
        }
        int result = bmsCostNoService.updateBmsCostNoByCostNo(bmsCostNo);
        if(result <= 0 ){
            result = bmsCostNoService.updateBmsCostNoHistoryByCostNo(bmsCostNo);
        }
        return toResult(response, result, map);
    }

    /**
     * 修改咨询报告号
     */
    @Log(title = "修改咨询报告号", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateReportNo",produces = "application/json;charset=utf-8")
    public ESBAjaxResult updateReferReportNo(HttpServletResponse response,@RequestBody Map<String, Object> map)
    {
        BmsReportNo bmsReportNo = new ObjectMapper().convertValue(map.get(ESBAjaxResult.ESB_REQ_DATA), BmsReportNo.class);
        if(StringUtils.isBlank(bmsReportNo.getReportNo())){
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
            return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), "咨询报告号不存在或不能为空"),null);
        }
        int result = bmsReportNoService.updateBmsReportNoByReportNo(bmsReportNo);
        return toResult(response, result, map);
    }

    /**
     * 修改服务范围
     */
    @Log(title = "修改服务范围", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateServiceArea",produces = "application/json;charset=utf-8")
    public ESBAjaxResult serviceArea(HttpServletResponse response,@RequestBody Map<String, Object> map)
    {
        BmsProject bmsProject = new ObjectMapper().convertValue(map.get(ESBAjaxResult.ESB_REQ_DATA), BmsProject.class);
        if(StringUtils.isBlank(bmsProject.getGlobalId())){
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
            return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR), "项目全局ID不存在或不能为空"),null);
        }
        //项目全局ID转大写
        bmsProject.setGlobalId(bmsProject.getGlobalId().toUpperCase());
        int result = bmsProjectMapper.updateBmsProjectByGlobalId(bmsProject);
        return toResult(response, result, map);
    }

    /**
     * 校验头部信息
     *
     * @param serviceName  服务请求方请求的ESB服务名称。ESB项目组提供
     * @param requestId  服务请求消息ID，ID消息流水号为UUID字符串
     * @param sourceSystem 请求系统
     * @param targetSystem 接收系统
     * @return
     */
   /* public boolean checkHttpHeaderParam(String serviceName, String requestId, String sourceSystem, String targetSystem){
        boolean flag = false;
        if(StringUtils.isBlank(serviceName)||esbServiceName.indexOf(serviceName)==-1
                ||StringUtils.isBlank(sourceSystem)||!sourceSystem.equals(esbSourceSystem)
                ||StringUtils.isBlank(targetSystem)||!targetSystem.equals(esbTargetSystem)
                ||StringUtils.isBlank(requestId)){
            flag = true;
        }
        return flag;
    }*/

    /**
     * 返回结果
     * @param map
     * @return
     */
    private ESBAjaxResult toResult(HttpServletResponse response, int result, Map<String, Object> map){
        if(result>=0){
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ONE.getCode());
            return ESBAjaxResult.success(AjaxResult.success());
        }else{
            response.addHeader(ESBAjaxResult.STATUS_FLAG, NumCode.ZERO.getCode());
            return ESBAjaxResult.error(new EsbResHead(String.valueOf(HttpStatus.ERROR),"更新失败"),
                    map.get(ESBAjaxResult.ESB_REQ_DATA));
        }
    }
}
